package com.houger.facade;

import com.houger.dto.AIAnswerDTO;
import com.houger.service.GptService;

import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author Houger.
 * @date 2024年02月07日 15:29.
 */
@RestController
@RequestMapping("/flux")
public class FluxTestFacade {


    @Resource
    GptService gptService;

    @GetMapping(value = "/getAnser", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<AIAnswerDTO>> getStream(@RequestParam("question") String question) {
        return gptService.doChatGPTStream(question)
            .map(aiAnswerDTO -> ServerSentEvent.<AIAnswerDTO>builder()
                .data(aiAnswerDTO).build())
            .onErrorResume(e -> Flux.empty());
    }
}
